<?php
/**
 * Dynamic menu  helper library.
 * Can be used with CSS from http://www.tanfa.co.uk/css/examples/menu/
 *
 * Methods to render dynamic menu .
 */
class MenuHelper extends AppHelper {
 
	var $helpers = array('Html');
 
	function renderMainMenu($menu = null, $active = null) {
		$out = '';

		foreach($menu as $caption => $config) {
			if(strtolower($caption)===strtolower($active)){
			$out = $out.'<li class="active"><a href="'.$this->webroot.'admin/'.$config[0].'"><span>'.str_replace("_", " ",$caption).'</span></a></li>';				
			} else
			$out = $out.'<li><a href="'.$this->webroot.'admin/'.$config[0].'"><span>'.str_replace("_", " ",$caption).'</span></a></li>';
		}
		$out = '<ul class="main">'.$out.'</ul>';
		$out = $this->Html->div('menu', $out);

		return $out;
		
	}
	
	function renderSubMenu($menu = null, $parent = null, $active = null) {
		$out = '';
		$subMenu = null;
		if((strtolower($parent)=='dashboard')||($active===null)){
			$out = '<ul class="submenu">'.$out.'</ul>';
			$out = $this->Html->div('sub', $out);			
		} else {		
			if(array_key_exists($parent,$menu)) {
				$subMenu=$menu[$parent]['child'];
			}
			foreach($subMenu as $caption => $config) {
				if(strtolower($caption)===strtolower($active)){
				$out = $out.'<li class="subactive"><a href="'.$this->webroot.'admin/'.$config.'"><span>'.str_replace("_", " ",$caption).'</span></a></li>';				
				} else
				$out = $out.'<li><a href="'.$this->webroot.'admin/'.$config.'"><span>'.str_replace("_", " ",$caption).'</span></a></li>';
			}
			$out = '<ul class="submenu">'.$out.'</ul>';
			$out = $this->Html->div('sub', $out);
		}
		return $out;
	}	
	
	function renderAllMenu($menu = null, $parent, $active = null) {
		$out = '';
		$out = $this->renderMainMenu($menu, $parent);
		$out .= $this->renderSubMenu($menu, $parent, $active);	
		return $out;
	}	
 
 
}
